var PenetList = {};
/**
 * 报表移动端首页
 */
PenetList.init = function(options){
	var params = PenetList._getParams_();
	PenetList._params_ = params;
	var $dom = $('.'+options.dom);
	PenetList.$dom = $dom;
	
	$dom.find(".discrip").html('<span>'+params.titleName+'</span> <span>:</span> <span class="color">'+params.titleValue+'</span>');
	PenetList._load_({"size":20,"page":1});
}
/**
 * 加载并渲染数据
 */
PenetList._load_ = function(options){
	var params = PenetList._params_;
	params.size = options.size;
	params.page = options.page;
	
	cmp.dialog.loading("数据加载中.....");
	rptApi.getStatsPenetrate({},params,{
		success:function(rs){
			PenetList._datas_ = rs;
			var displayFields = JSON.parse( rs.penetrateInfo.showFields);
			var  wd = window.innerWidth,dflength = displayFields.length;
			PenetList._cellStyle_ = '';
			if(dflength * 100 < wd){//当页无法撑开的时候设置固定的百分比
				PenetList._cellStyle_ = "width:" + Math.floor(wd/dflength) + "px";
			}
			//渲染表头
			var header = PenetList.headTpl.render({
				displayFields:displayFields,
				"cellStyle":PenetList._cellStyle_
			});
			PenetList.$dom.find(".qt-tb-header tbody").html(header);
			
			var data = rs.data;
			var dataLength = data.length;
			if(dataLength == 0){
				PenetList.$dom.find(".qt-tb-body tbody").html("");
				PenetList.$dom.find(".qt-tb-footer .pages").html("-/-");
			}else{
				var header = "",
					serverAddr = cmp.serverIp ? cmp.serverIp : "";
				for (var index = 0; index < dataLength; index++) {
					var row = data[index];
					header += '<tr row-num="' + index + '" >';
					for(var j=0;j < dflength;j++){
						var df = displayFields[j];
						var cellVal = row[df.aliasTableName + "_" + df.name ];
						var showValue = "";
						if(cellVal != null && cellVal != undefined){
							showValue = PenetList.toHtml(cellVal);
						}

						header += '<td ><div class="qt-tb-cell type-' + df.fieldComType+ '" style="' + PenetList._cellStyle_ + '">' + showValue + '</div></td>';
					}
					header += '</tr>';
				}
				PenetList.$dom.find(".qt-tb-body tbody").html(header);
				PenetList.$dom.find(".qt-tb-footer .pages").html(rs.page + "/" + rs.pages + "页");
				PenetList.bindEvent();
			}
			cmp.dialog.loading(false);
		},
		error:function(error){
			cmp.dialog.loading(false);
			var cmpHandled = cmp.errorHandler(error);
			if (!cmpHandled) {
				if (error.message) {
					PenetList.alert(error.message);
				} else {
					PenetList.alert(cmp.toJSON(error));
				}
			}
		}
	});
}
PenetList.bindEvent = function() {
	var $dom = PenetList.$dom;
	$dom.off("click",".qt-tb-body tbody tr").on("click",".qt-tb-body tbody tr",function(){
		var rowIndex = $(this).attr("row-num");
		var rowData = PenetList._datas_.data[rowIndex];
		
		var penetrateInfo = PenetList._datas_.penetrateInfo;
		var dataKey = penetrateInfo.dataKey;
		penetrateInfo.moduleId = rowData[dataKey];
		switch (penetrateInfo.category) {
			case "form":
			{
				//com.seeyon.ctp.form.util.Enums.FormType
				if(penetrateInfo.formType == 1){//流程表单
					cmp.asyncLoad.js([collaborationPath + "/collaboration_m_api.s3js" + buildversion],function(){
						collApi.openSummary({
							"summaryId":penetrateInfo.moduleId,
							"operationId":penetrateInfo.rightId,
							"formId":penetrateInfo.formId,
							"openFrom":"formStatistical"
						});
					});
				}else if(penetrateInfo.formType == 2 || penetrateInfo.formType == 3){//无流程表单
					cmp.asyncLoad.js([unflowformPath + "/unflowform_m_api.s3js"  + buildversion],function(){
						cmp.openUnflowFormData({
							"moduleType":37,
							"moduleId":penetrateInfo.moduleId,
							"name":penetrateInfo.formName,
							"rightId":penetrateInfo.rightId,
							"viewState":2,
							"formId":penetrateInfo.formId,
							"needCheckRight":true//检查权限
						});
					});
				}else{
					cmp.notification.toast("sorry 暂不支持穿透","center");
				}
				break;
			}
			case "excelreport":
			{
				cmp.asyncLoad.js([vreportPath + "/vreport_m_api.s3js" + buildversion],function(){
					vreportApi.viewExcelDetail(penetrateInfo);
				});
				break;
			}
			case "cap4biz":
			case "cap4Form"://cap4
			{
				//com.seeyon.ctp.form.util.Enums.FormType
				if(penetrateInfo.formType == 1){//流程表单
					cmp.asyncLoad.js([collaborationPath + "/collaboration_m_api.s3js" + buildversion],function(){
						collApi.openSummary({
							"summaryId":penetrateInfo.moduleId,
							"operationId":penetrateInfo.rightId,
							"formId":penetrateInfo.formId,
							"designId":PenetList._params_.designId,
							"openFrom":"capStatistical"
						});
					});
				}else if(penetrateInfo.formType == 6){//无流程表单
					cmp.asyncLoad.js([cap4Path + "/cap4_m_api.s3js" + buildversion],function(){
						cap4Api.openUnFlow({
							formType:(penetrateInfo.includeLight ? "light" : "origin" ),// light | origin 表单类型
							type:"browse",// new | edit | browse 操作类型
							title:penetrateInfo.formName,
							params:{
								formTemplateId: penetrateInfo.formId,
								moduleId: penetrateInfo.moduleId,
								designId:PenetList._params_.designId,
								moduleType: "42",
								operateType:"2",//0 = 新建 1 = 编辑 2= 浏览
								rightId:penetrateInfo.rightId
							}
						},{
							openWebViewCatch:1
						});
					});
				}else{
					cmp.notification.toast("sorry 暂不支持穿透","center");
				}
				break;
			}
			default:
				cmp.notification.toast("sorry  暂不支持穿透","center");
				break;
		}
	}).off("click",".prePage").on("click",".prePage",function(){//上一页
		var page = parseInt(PenetList._datas_.page) - 1;
		if(page > 0){
			PenetList._load_({
				"size":20,
				"page":page
			});
		}
	}).off("click",".nextPage").on("click",".nextPage",function(){//下一页
		var page = parseInt(PenetList._datas_.page) + 1;
		if(page <= PenetList._datas_.pages){
			PenetList._load_({
				"size":20,
				"page":page
			});
		}
	});
	
}
/** 获取URL的查询参数 */
PenetList._getParams_ = function(){
	var url = window.location.search,
	reg_url = /^\?([\w\W]+)$/,
	reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
	arr_url = reg_url.exec(url),
	params = {};
	if (arr_url && arr_url[1]) {
		var str_para = arr_url[1], result;
		while ((result = reg_para.exec(str_para)) != null) {
			params[result[1]] = result[2];
		}
	}
	var cmpparams = cmp.href.getParam();
	if(cmpparams){
		params = $.extend(params,cmpparams)
	}
    return params;
}

PenetList.toHtml = function(a) {
	try {
		return a ? (a = a.replace(/&/g, "&amp;"),
		a = a.replace(/</g, "&lt;"),
		a = a.replace(/>/g, "&gt;"),
		a = a.replace(/\'/g, "&#039;"),
		a = a.replace(/"/g, "&#034;"),
		a = a.replace(/ /g, "&nbsp;"),
		a) : ""
	} catch (error) {
		console.log("toHtml",a);
		return "";
	}
}
PenetList.alert = function(tips){
	cmp.notification.alert(tips);
}
PenetList.headTpl = laytpl('\
	<tr >\
	{{#\
		for(var i=0;i<d.displayFields.length;i++){\
			var displayField = d.displayFields[i];\
	}}\
		<td ><div class="qt-tb-cell type-{{=displayField.fieldComType }}" style="{{d.cellStyle}}">{{=displayField.display }}</div></td>\
	{{#\
		}\
	}}\
	</tr>\
');
